package xx.observer.design;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname ConcreteSubject
 * @Description TODO  具体主题
 * @Date 2023/7/25 14:36
 * @Created by wangxianlin
 */
public class ConcreteSubject  implements Subject {

    private List<Observer> observers = new ArrayList<>();
    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyObservers();
    }

    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(this);
        }
    }
}
